/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.controller.section;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fujitsu.nanjing.tieba.base.BaseController;
import com.fujitsu.nanjing.tieba.constrant.CommonParamter;
import com.fujitsu.nanjing.tieba.domain.BoardInfo;
import com.fujitsu.nanjing.tieba.exception.Resource404Exception;
import com.fujitsu.nanjing.tieba.service.BoardInfoService;
import com.fujitsu.nanjing.tieba.service.SectionInfoService;

/**
 * BoardController.java
 * 
 * @author Qixiaobo-win8
 */
@Controller
@RequestMapping("/forum/boards")
public class BoardController extends BaseController {
	@Autowired
	private BoardInfoService boardInfoService;
	@Resource
	private SectionInfoService sectionInfoService;
	@RequestMapping(value = "/{boardid}", method = RequestMethod.GET)
	public String showBoard(Model model, @PathVariable String boardid)
			throws Resource404Exception {
		return redirect("/forum/boards/" + boardid + "/pages/1");
	}

	@RequestMapping(value = "/{boardid}/digist", method = RequestMethod.GET)
	public String showBoardInDigist(Model model, @PathVariable String boardid)
			throws Resource404Exception {
		return redirect("/forum/boards/" + boardid + "/digist/pages/1");
	}
	@RequestMapping(value = "/{boardid}/reward", method = RequestMethod.GET)
	public String showBoardInReward(Model model, @PathVariable String boardid)
			throws Resource404Exception {
		return redirect("/forum/boards/" + boardid + "/reward/pages/1");
	}

	@RequestMapping(value = "/{boardid}/pages/{pageNum}", method = RequestMethod.GET)
	public String showBoardWithPageNum(Model model,
			@PathVariable String boardid, @PathVariable String pageNum)
			throws Resource404Exception {
		boardid = decrypt(boardid);
		BoardInfo boardInfo = boardInfoService.findByIdWithPageNum(boardid,
				Integer.parseInt(pageNum));
		if (boardInfo == null) {
			throw new Resource404Exception("404,你懂的！");
		}
		pageNum = (pageNum == null ? "0" : pageNum);
		model.addAttribute("pageIndex", pageNum);
		model.addAttribute("boardInfo", boardInfo);
		model.addAttribute("boardType", "all");
		model.addAttribute("sectionInfos", sectionInfoService.findAll());
		return "board/board";
	}
	@RequestMapping(value = "/{boardid}/digist/pages/{pageNum}", method = RequestMethod.GET)
	public String showBoardWithPageNumInDigist(Model model,
			@PathVariable String boardid, @PathVariable String pageNum)
			throws Resource404Exception {
		boardid = decrypt(boardid);
		BoardInfo boardInfo = boardInfoService.findByIdWithPageNumInType(
				boardid, Integer.parseInt(pageNum), CommonParamter.精华);
		if (boardInfo == null) {
			throw new Resource404Exception("404,你懂的！");
		}
		pageNum = (pageNum == null ? "0" : pageNum);
		model.addAttribute("pageIndex", pageNum);
		model.addAttribute("boardInfo", boardInfo);
		model.addAttribute("boardType", "digist");
		model.addAttribute("sectionInfos", sectionInfoService.findAll());
		return "board/board";

	}
	@RequestMapping(value = "/{boardid}/reward/pages/{pageNum}", method = RequestMethod.GET)
	public String showBoardWithPageNumInReward(Model model,
			@PathVariable String boardid, @PathVariable String pageNum)
			throws Resource404Exception {
		boardid = decrypt(boardid);
		BoardInfo boardInfo = boardInfoService.findByIdWithPageNumInType(
				boardid, Integer.parseInt(pageNum), CommonParamter.悬赏);
		if (boardInfo == null) {
			throw new Resource404Exception("404,你懂的！");
		}
		pageNum = (pageNum == null ? "0" : pageNum);
		model.addAttribute("pageIndex", pageNum);
		model.addAttribute("boardInfo", boardInfo);
		model.addAttribute("boardType", "reward");
		model.addAttribute("sectionInfos", sectionInfoService.findAll());
		return "board/board";

	}

}
